<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="空间名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入空间名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:cameraGroup:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cameraGroupList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="空间表id" align="center" prop="id" v-if="false"/>
      <el-table-column label="空间ID" align="center" prop="groupId" />
      <el-table-column label="空间名称" align="center" prop="name" />
      <el-table-column label="空间描述" align="center" prop="description" />
      <el-table-column label="服务中心" align="center" prop="region" />
      <el-table-column label="应用名称" align="center" prop="app" />
      <el-table-column label="接入协议" align="center" prop="inProtocol" :formatter="inProtocolFormat" />
      <el-table-column label="播放协议" align="center" prop="outProtocol" />
      <el-table-column label="是否启用" align="center" prop="enabled" />
      <el-table-column label="空间状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="推流域名" align="center" prop="pushDomain" />
      <el-table-column label="播流域名" align="center" prop="playDomain" />
      <el-table-column label="国标ID" align="center" prop="gbId" />
      <el-table-column label="国标信令服务器地址" align="center" prop="gbIp" />
      <el-table-column label="UDP端口" align="center" prop="gbUdpPort" />
      <el-table-column label="TCP端口" align="center" prop="gbTcpPort" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监控空间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true"  label-width="120px">
        <el-form-item label="空间ID" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入空间ID" />
        </el-form-item>
        <el-form-item label="空间名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入空间名称" />
        </el-form-item>
        <el-form-item label="空间描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入空间描述" />
        </el-form-item>
        <el-form-item label="服务中心" prop="region">
          <el-input v-model="form.region" placeholder="请输入服务中心" />
        </el-form-item>
        <el-form-item label="应用名称" prop="app">
          <el-input v-model="form.app" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="接入协议" prop="inProtocol">
          <el-select v-model="form.inProtocol" placeholder="请选择接入协议">
            <el-option
              v-for="dict in inProtocolOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="播放协议，多值用逗号分隔" prop="outProtocol">
          <el-input v-model="form.outProtocol" placeholder="请输入播放协议" />
        </el-form-item>
        <el-form-item label="空间是否启用" prop="enabled">
          <el-input v-model="form.enabled" placeholder="请输入空间是否启用" />
        </el-form-item>
        <el-form-item label="空间状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="推流域名" prop="pushDomain">
          <el-input v-model="form.pushDomain" placeholder="请输入推流域名" />
        </el-form-item>
        <el-form-item label="播流域名" prop="playDomain">
          <el-input v-model="form.playDomain" placeholder="请输入播流域名" />
        </el-form-item>
        <el-form-item label="国标ID" prop="gbId">
          <el-input v-model="form.gbId" placeholder="请输入空间内设备状态更新时的回调" />
        </el-form-item>
        <el-form-item label="国标信令服务器地址" prop="gbIp">
          <el-input v-model="form.gbIp" placeholder="请输入空间提供的国标ID。" />
        </el-form-item>
        <el-form-item label="UDP端口" prop="gbUdpPort">
          <el-input v-model="form.gbUdpPort" placeholder="请输入UDP端口" />
        </el-form-item>
        <el-form-item label="TCP端口" prop="gbTcpPort">
          <el-input v-model="form.gbTcpPort" placeholder="请输入TCP端口" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCameraGroup, getCameraGroup, delCameraGroup, addCameraGroup, updateCameraGroup, exportCameraGroup } from "@/api/system/cameraGroup";

export default {
  name: "CameraGroup",
  components: {
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监控空间表格数据
      cameraGroupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 接入协议字典
      inProtocolOptions: [],
      // 空间状态。字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("camera_group_in_protocol").then(response => {
      this.inProtocolOptions = response.data;
    });
    this.getDicts("camera_group_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询监控空间列表 */
    getList() {
      this.loading = true;
      listCameraGroup(this.queryParams).then(response => {
        this.cameraGroupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 接入协议字典翻译
    inProtocolFormat(row, column) {
      return this.selectDictLabel(this.inProtocolOptions, row.inProtocol);
    },
    // 空间状态。字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        groupId: undefined,
        name: undefined,
        description: undefined,
        region: undefined,
        app: undefined,
        inProtocol: undefined,
        outProtocol: undefined,
        enabled: undefined,
        status: "0",
        pushDomain: undefined,
        playDomain: undefined,
        gbId: undefined,
        gbIp: undefined,
        gbPort: undefined,
        gbUdpPort: undefined,
        gbTcpPort: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监控空间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCameraGroup(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改监控空间";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCameraGroup(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCameraGroup(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除监控空间编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delCameraGroup(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有监控空间数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCameraGroup(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
